<?php

namespace App\Logics;


use App\Models\User;
use App\Models\UserAuth;
use App\Service\Base\Logic;
use App\Service\WeChat\OpenWx;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;

class UserLogic extends Logic
{

    // 详情
    public function info($myUid)
    {
        $userDb = new User();
        $uid    = $myUid ?: $this->option()->getData('uid', 0, 'intval');
        $info   = $userDb->isCache(true)->oneFind(['id' => $uid]);
        $auth   = [];
        if($info['auth_id']){
            $auth = (new UserAuth())->isCache(true)->getOneById($info['auth_id']);
        }


        // 头像
        $info['auth']  = $auth;
        $info['cover'] = $auth['cover'] ?? '';
        //$info['head_img'] = $this->getHeadImg($info['head_img']);
        //$info['qrcode_url'] = (new \App\Service\QRcode())->getQR($info['id']);
        // 如果是获取认证信息，只返回特定的几个参数


        return $info;
    }


    // 完善个人资料
    public function profile($myUid)
    {
        $opt  = $this->option();
        $data = [
            'head_img' => $opt->getData('head_img', null, 'trim'),
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'mobile'   => $opt->getData('mobile', null, 'trim'),
            // 以下待添加
            'sign'     => $opt->getData('sign', null, 'trim'),
            'birthday' => $opt->getData('birthday', null, 'trim'),
            'sex'      => $opt->getData('sex', null, 'trim'),
        ];

        // 头像
        /*if($data['head_img'] && (strpos($data['head_img'], '?imageMogr2/auto-orient/thumbnail/100x100') === false)){
            $data['head_img'] = $this->showQiniuImage($data['head_img'], 100, 100);
        }*/
        if($data['birthday']){
            $data['birthday'] = strtotime($data['birthday']);
        }

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请传递需要修改的数据！');
        }

        // 判断手机号是否已经存在
        $userDb = new User();
        if(!empty($data['mobile'])){
            $map = [['mobile', '=', $data['mobile']], ['status', '>=', 0], ['id', '<>', $myUid]];
            $has = $userDb->oneFind($map);
            if($has){
                return $this->_returnFail('对不起，该手机号已经存在！');
            }
        }

        // 执行修改
        $result = $userDb->isCache(true)->oneEdit(['id' => $myUid], $data);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        // 删除缓存
        $userDb->deleteCacheShow(['id' => $myUid], $data);

        // 返回数据
        $info = $userDb->isCache(true)->oneFind(['id' => $myUid]);
        // 更新用户名 昵称
        if(!empty($data['mobile']) && empty($info['nickname'])){
            // 更新
            $username = $info['salt'].substr($info['id'] + $data['mobile'], -4);
            $updata   = ['nickname' => $username];
            if(empty($info['username'])){
                $updata['username'] = $username;
            }
            $rst = $userDb->where('id', $myUid)->update($updata);
            if($rst === false){
                return $this->_returnFail($userDb->getError());
            }

            // 删除缓存
            $userDb->deleteCacheShow(['id' => $myUid], $data);

            // 返回数据
            $info = $userDb->isCache(true)->oneFind(['id' => $myUid]);
        }

        return $info;
    }


    // 认证操作


    // 我的认证资料


    // 注销
    public function cancel($myUid)
    {
        $userDb = new User();
        //$result = $userDb->isCache(true)->cancel($myUid);
        $result = $userDb->isCache(true)->oneEdit(['id' => $myUid], ['status' => -1]);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        return $result;
    }


    // 修改密码
    public function onPassword($myUid = 0)
    {
        if(empty($myUid)){
            return $this->_returnFail('请先登录在操作！');
        }

        $option    = $this->option();
        $old_pass  = $option->getData('old_pass', null, 'trim');
        $new_pass  = $option->getData('new_pass', null, 'trim');
        $new_pass2 = $option->getData('new_pass2', null, 'trim');
        if(empty($new_pass)){
            return $this->_returnFail('请输入密码！');
        }
        if(empty($new_pass2)){
            return $this->_returnFail('请输入确认密码！');
        }
        if($new_pass != $new_pass2){
            return $this->_returnFail('两次输入密码不一样！');
        }
        if($new_pass == $old_pass){
            return $this->_returnFail('新密码与旧密码一样！');
        }

        // 获取登录的用户信息
        $userDb = new User();
        $where  = ['id' => $myUid];
        $info   = $userDb->isCache(true)->oneFind($where);
        if($info === false){
            return $this->_returnFail($userDb->getError());
        }
        if(empty($info)){
            return $this->_returnFail('用户不存在或者已经被禁用！');
        }

        // 判断旧密码
        $salt = $info['salt'];
        $_res = $userDb->ckeckPassword($old_pass, $salt, $info['password']);
        if($_res === false){
            return $this->_returnFail('旧密码错误！');
        }

        // 如果没有 则注册
        // $salt  = Str::random(4);
        $_pass  = $userDb->add_md5($new_pass, $salt);  // 密码
        $result = $userDb->where($where)->update(['password' => $_pass]);

        // 缓存操作
        $userDb->deleteCacheShow($info);

        return $result;
    }


    // 验证登录用户的密码
    public function checkPassword($myUid)
    {
        $option   = $this->option();
        $password = $option->getData('password', null, 'trim');

        // 验证密码
        $userDb = new User();
        $info   = $userDb->isCache(true)->oneFind(['id' => $myUid]);
        $info   = is_object($info) ? $info->toArray() : $info;
        $res    = $userDb->ckeckPassword($password, $info['salt'], $info['password']);
        if(!$res){
            return $this->_returnFail('密码错误！！');
        }

        return true;
    }

}


